package com.cn.travel.web.portal;


import com.alibaba.fastjson.JSONObject;
import com.zhenzi.sms.ZhenziSmsClient;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.Map;
import java.util.Random;

@Controller
public class CodeController {
	//短信平台相关参数
	//这个不用改
	private String apiUrl = "https://sms_developer.zhenzikj.com";
	//榛子云系统上获取
	private String appId = "113890";
	private String appSecret = "55bb452b-c89d-45cd-acd3-fc9f65cc098b";

	@ResponseBody
	@GetMapping("/fitness/code")
	public boolean getCode(String memPhone, HttpSession httpSession){
		try {
			JSONObject json = null;
			//随机生成验证码
			String code = String.valueOf(new Random().nextInt(999999));
			//将验证码通过榛子云接口发送至手机
			ZhenziSmsClient client = new ZhenziSmsClient(apiUrl, appId, appSecret);
			Map<String, Object> params = new HashMap<String, Object>();
			//前台输入的手机号
			params.put("number", memPhone);
			//这个模板id对应的是榛子云个人中心的模板id
			params.put("templateId", 12891);
			String[] templateParams = new String[2];
			templateParams[0] = code;
			templateParams[1] = "5分钟";
			params.put("templateParams", templateParams);
			String result = client.send(params);
			System.out.println(result);
			json = JSONObject.parseObject(result);
			if (json.getIntValue("code")!=0){//发送短信失败
				return  false;
			}
			//将验证码存到session中,同时存入创建时间
			//以json存放，这里使用的是阿里的fastjson
			json = new JSONObject();
			json.put("memPhone",memPhone);
			json.put("code",code);
			json.put("createTime",System.currentTimeMillis());
			// 将认证码存入SESSION
			httpSession.setAttribute("code",json);
			return true;
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		}
	}
}
